package com.link.linkupjavaweb.service.impl;

import com.link.linkupjavaweb.dto.AdminLoginDTO;
import com.link.linkupjavaweb.entity.Admin;
import com.link.linkupjavaweb.mapper.AdminMapper;
import com.link.linkupjavaweb.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * 管理员Service实现类
 */
@Service
public class AdminServiceImpl implements AdminService {
    
    @Autowired
    private AdminMapper adminMapper;
    
    @Override
    public Admin login(AdminLoginDTO loginDTO) {
        // 1. 根据用户名查询管理员
        Admin admin = adminMapper.selectByUsername(loginDTO.getUsername());
        
        // 2. 验证管理员是否存在
        if (admin == null) {
            throw new RuntimeException("管理员账号不存在");
        }
        
        // 3. 验证账号是否被禁用
        if (admin.getStatus() == 1) {
            throw new RuntimeException("该账号已被禁用");
        }
        
        // 4. 验证密码
        if (!admin.getPassword().equals(loginDTO.getPassword())) {
            throw new RuntimeException("密码错误");
        }
        
        // 5. 更新最后登录时间
        admin.setLastLoginTime(LocalDateTime.now());
        adminMapper.updateById(admin);
        
        return admin;
    }
    
    @Override
    public Admin getAdminById(Long id) {
        return adminMapper.selectById(id);
    }
}

